package cqrtplm.service.impl;


import java.sql.Timestamp;
import java.util.Date;

import cqrtplm.log.ObjectComparator;
import cqrtplm.log.RTLogDifference;
import cqrtplm.mapper.RTLogDifferenceMapper;
import cqrtplm.service.IRTLogDifferenceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RTLogDifferenceServiceImpl implements IRTLogDifferenceService {
    @Resource
    private RTLogDifferenceMapper  rtLogDifferenceMapper;
    @Override
    public <T> int newLog(T oldObj, T newObj, String keyvalue, String modifier,String modifySoure,String modifyType,String remarks)   {
        ObjectComparator.CompareResult diffrenceResult=ObjectComparator.compare(oldObj, newObj, modifier);

        if(!diffrenceResult.isNoChange()) {
            RTLogDifference rtlogdifference = new RTLogDifference();
            rtlogdifference.setEntityClass(diffrenceResult.getClassName());
            rtlogdifference.setEntityName(diffrenceResult.getClassNameComment());
            rtlogdifference.setEntityId(keyvalue);
            rtlogdifference.setFCreateDate(new Timestamp(new Date().getTime()));
            rtlogdifference.setFCreateUserId(modifier);
            rtlogdifference.setModifySoure(modifySoure);
            rtlogdifference.setModifyType(modifyType);
            rtlogdifference.setRemarks(remarks);
            rtlogdifference.setDifferencesStr(diffrenceResult.getDetailsAsJson());

            return rtLogDifferenceMapper.newlog(rtlogdifference);
        }else{
            return 0;
        }
    }

}
